<?php

namespace addon\hotel\api\controller;

use addon\hotel\model\Room;
use app\api\controller\BaseApi;
use addon\hotel\model\OrderCreate as OrderCreateModel;

class Ordercreate extends BaseApi
{

    //提交订单
    public function create()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $data = [
            'member_id' => $this->member_id,
            'room_id' => isset($this->params['room_id']) ? $this->params['room_id'] : 0,
            'room_num' => isset($this->params['room_num']) ? $this->params['room_num'] : 0,
            'arrival_time' => isset($this->params['arrival_time']) ? $this->params['arrival_time'] : '',
            'mobile' => isset($this->params['mobile']) ? $this->params['mobile'] : '',
            'start_date' => isset($this->params['start_date']) ? $this->params['start_date'] : '',
            'end_date' => isset($this->params['end_date']) ? $this->params['end_date'] : '',
            'is_balance' => isset($this->params[ 'is_balance' ]) ? $this->params[ 'is_balance' ] : 0,//是否使用余额
            'is_point'        => isset($this->params['is_point']) ? $this->params['is_point'] : 1,//是否使用积分
            'realname'           => isset($this->params['realname']) ? $this->params['realname'] : '',
        ];

        if (empty($data[ 'room_id' ]) && empty($data[ 'room_id' ])) {
            return $this->response($this->error('', '缺少必填参数房间数据'));
        }

        if (empty($data[ 'mobile' ]) && empty($data[ 'mobile' ])) {
            return $this->response($this->error('', '请输入联系电话'));
        }

        if (empty($data[ 'arrival_time' ]) && empty($data[ 'arrival_time' ])) {
            return $this->response($this->error('', '请选择到达时间'));
        }

        $order_create_model = new OrderCreateModel();

        $res = $order_create_model->create($data);

        return $this->response($res);

    }


    //订单提交页计算
    public function calculate()
    {
        $token = $this->checkToken();
        if ($token['code'] < 0) return $this->response($token);

        $data = [
            'member_id' => $this->member_id,
            'room_id' => isset($this->params['room_id']) ? $this->params['room_id'] : 0,
            'room_num' => isset($this->params['room_num']) ? $this->params['room_num'] : 0,
            //'arrival_time' => isset($this->params['arrival_time']) ? $this->params['arrival_time'] : '',
            //'mobile' => isset($this->params['mobile']) ? $this->params['mobile'] : '',
            'start_date' => isset($this->params['start_date']) ? $this->params['start_date'] : '',
            'end_date' => isset($this->params['end_date']) ? $this->params['end_date'] : '',
            'is_balance' => isset($this->params[ 'is_balance' ]) ? $this->params[ 'is_balance' ] : 0,//是否使用余额
            'is_point'        => isset($this->params['is_point']) ? $this->params['is_point'] : 1,//是否使用积分
            //'point'           => isset($this->params['point']) ? $this->params['point'] : 0,
        ];

        if (empty($data[ 'room_id' ]) && empty($data[ 'room_id' ])) {
            return $this->response($this->error('', '缺少必填参数房间数据'));
        }

        /*if (empty($data[ 'mobile' ]) && empty($data[ 'mobile' ])) {
            return $this->response($this->error('', '请输入联系电话'));
        }

        if (!validatePhoneNumber($data[ 'mobile' ])) {
            return $this->response($this->error('', '联系电话格式有误'));
        }

        if (empty($data[ 'arrival_time' ]) && empty($data[ 'arrival_time' ])) {
            return $this->response($this->error('', '请选择到达时间'));
        }*/

        $order_create_model = new OrderCreateModel();

        $res = $order_create_model->calculate($data);

        return $this->response($res);

    }


    //库存提示
    public function stockWarning()
    {
        $room_id = isset($this->params['room_id']) ? $this->params['room_id'] : 0;
        $room_num = isset($this->params['room_num']) ? $this->params['room_num'] : 0;
        $start_date = isset($this->params['start_date']) ? $this->params['start_date'] : '';
        $end_date = isset($this->params['end_date']) ? $this->params['end_date'] : '';

        $room_model = new Room();

        $room_info = $room_model->getRoomInfo($room_id);
        $room_info = $room_info['data'];

        $date = [];

        $allday = daysBetweenDates($start_date,$end_date);

        for($i=0;$i<$allday;$i++){

            $begin = date_to_time($start_date)+$i*86400;
            $end = date_to_time($end_date)+$i*86400+86399;
            $day = date('Y-m-d',$begin);

            $order_count = \think\facade\Db::name('hotel_order')
                ->where('room_id', $room_id)
                //->where('pay_status', 1)
                ->where('order_status', '>',0)
                ->where('start_date_timestamp', '<=',$begin)
                ->where('end_date_timestamp', '>=',$end)
                ->sum('room_num');

            if ($order_count>=$room_info['stock']){
                array_push($date,$day);
            } elseif($room_num>=$room_info['stock']) {
                array_push($date,$day);
            }
        }

        if (count($date)>0){
            return $this->response($this->error('',implode(',',$date).'日期库存不足'));
        }

        return $this->response($this->success(1));
    }
}